﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FlyHorse.LIMS.Model;
using FlyHorse.LIMS.WinApp.LanguageResource.AuthorityManageForms;
using FlyHorse.LIMS.WinApp.LanguageResource;

namespace FlyHorse.LIMS.WinApp.AuthorityManageForms.UserForms
{
    public partial class LoginUserDetailForm : DevExpress.XtraEditors.XtraForm
    {
        Action _InitGrid;
        int _option;
        LoginUser _loginUser;
        BLL.LoginUserBll loginUserBll = new BLL.LoginUserBll();
        BLL.ToolBll toolBll = new BLL.ToolBll();
        public LoginUserDetailForm(Action InitGrid)
        {
            InitializeComponent();
            _option = 1;
            _InitGrid = InitGrid;
        }

        public LoginUserDetailForm(LoginUser loginUser, Action InitGrid)
        {
            InitializeComponent();
            _option = 2;
            _loginUser = loginUser;
            _InitGrid = InitGrid;
        }

        private void LoginUserDetailForm_Load(object sender, EventArgs e)
        {
            this.checkedComboBoxEdit_roles.Properties.DataSource = toolBll.GetRoleList();
            if (_option == 1)
            {
                this.Text = ResourceAuthority.CreateUser;
            }
            if (_option == 2)
            {
                this.Text = ResourceAuthority.EditUser;
                this.textEdit_UserName.Text = _loginUser.UserName;
                this.textEdit_Password.Text = _loginUser.Password;
                this.textEdit_EmpName.Text = _loginUser.EmpName;
                this.textEdit_Remark.Text = _loginUser.Remark;
                this.checkedComboBoxEdit_roles.EditValue = _loginUser.RoleID;
            }
        }

        private void simpleButton_Save_Click(object sender, EventArgs e)
        {
            //判空
            if (this.textEdit_UserName.Text == "")
            {
                XtraMessageBox.Show(ResourceAuthority.UserNameNull);
                textEdit_UserName.Focus();
                return;
            }
            if (this.textEdit_Password.Text == "")
            {
                XtraMessageBox.Show(ResourceAuthority.PasswordNull);
                textEdit_Password.Focus();
                return;
            }
            if (this.textEdit_EmpName.Text == "")
            {
                XtraMessageBox.Show(ResourceAuthority.FullNameNull);
                textEdit_EmpName.Focus();
                return;
            }
            if (this.checkedComboBoxEdit_roles.EditValue==null)
            {
                XtraMessageBox.Show(ResourceAuthority.RoleNull);
                checkedComboBoxEdit_roles.Focus();
                return;
            }
            if (_option == 1)
            {
                LoginUser loginUser = new LoginUser();
                loginUser.EmpName = this.textEdit_EmpName.Text;
                loginUser.IsDelete = false;
                loginUser.Password = this.textEdit_Password.Text;
                loginUser.Remark = this.textEdit_Remark.Text;
                loginUser.RoleID = Convert.ToString(this.checkedComboBoxEdit_roles.EditValue);
                loginUser.UserName = this.textEdit_UserName.Text;
                loginUser.LoginUserID = Guid.NewGuid().ToString();

                //判重
                int count = loginUserBll.LoadEntities(m => m.UserName == loginUser.UserName && m.IsDelete == false).Count();
                if (count > 0)
                {
                    XtraMessageBox.Show(ResourceAuthority.NameExists);
                    return;
                }

                var res = loginUserBll.AddEntity(loginUser);
                if (res != null)
                {
                    _InitGrid();
                    XtraMessageBox.Show(ResourceOverall.CreateSucceed);
                    this.Close();
                }
            }
            if (_option == 2)
            {
                using (OJCB4Entities db =new OJCB4Entities())
                {
                    LoginUser loginUser = db.LoginUsers.Find(_loginUser.LoginUserID);
                    if (loginUser!=null)
                    {
                        loginUser.EmpName = this.textEdit_EmpName.Text;
                        loginUser.IsDelete = false;
                        loginUser.Password = this.textEdit_Password.Text;
                        loginUser.Remark = this.textEdit_Remark.Text;
                        loginUser.RoleID = Convert.ToString(this.checkedComboBoxEdit_roles.EditValue);
                        loginUser.UserName = this.textEdit_UserName.Text;
                    }
                    bool res = db.SaveChanges() > 0;
                    if (res)
                    {
                        _InitGrid();
                        XtraMessageBox.Show(ResourceOverall.EditSucceed);
                        this.Close();
                    }
                }
            }
        }
    }
}